package com.suning.framework.redis;

import com.suning.framework.sedis.ShardedJedisAction;
import com.suning.framework.sedis.impl.ShardedJedisClientImpl;
import redis.clients.jedis.MultiKeyCommands;
import redis.clients.jedis.ShardedJedis;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class RedisTest {

    private static ShardedJedisClientImpl shardedJedisClient;

    static {
        shardedJedisClient = new ShardedJedisClientImpl("redis.conf");
    }

    /**
     * 插入单个值
     * @param key
     * @param value
     * @return
     */
    public String set(final String key, final String value,final int second) {
        return shardedJedisClient.execute(new ShardedJedisAction<String>() {
            public String doAction(ShardedJedis shardedJedis) {
                shardedJedis.expire(key,second);
                return shardedJedis.set(key, value);
            }
        });
    }

    /**
     * 取出值
     */
    public String get(final String key) {
        return shardedJedisClient.execute(new ShardedJedisAction<String>() {
            public String doAction(ShardedJedis shardedJedis) {
                return shardedJedis.get(key);
            }
        });
    }
    /**
     * 插入多个值
     * @param keyStr
     * @return
     */
    public String set(final String... keyStr) {
        return shardedJedisClient.execute(new ShardedJedisAction<String>() {
            public String doAction(ShardedJedis shardedJedis) {
                MultiKeyCommands shardCommands = (MultiKeyCommands) shardedJedis;
                return shardCommands.mset(keyStr);
            }
        });
    }

    public static void main(String[] args) {

        RedisTest redisTest = new RedisTest();
        //redisTest.set("linjian","123",1);
        System.out.println(redisTest.get("linjian"));


    }
}
